package com.itheima.service;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.pojo.ReportData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = ReportService.class)
@Transactional
public class ReportServiceImpl implements ReportService {

    @Autowired
    MemberDao memberDao;

    @Autowired
    OrderDao orderDao;


    @Override
    public List<Integer> getMemberReport(List<String> months) {
        List<Integer> memberCount = new ArrayList<>();
        for (String month : months) {//2019-04
            month = month + "-31";
            Integer count = memberDao.getMemberReport(month);//2019-04-31
            memberCount.add(count);
        }
        return memberCount;
    }

    @Override
    public List<Map> getSetmealReport() {
        return orderDao.getSetmealReport();
    }

    @Override
    public ReportData getBusinessReportData() {
        String format = "yyyy-MM-dd";
        DateTime now = new DateTime();
        String nowStr = now.toString(format);
        String beginOfWeek = DateUtil.beginOfWeek(now).toString(format);//2020-04-13 00:00:00
        String endOfWeek = DateUtil.endOfWeek(now).toString(format);//2020-04-19 23:59:59
        String beginOfMonth = DateUtil.beginOfMonth(now).toString(format);//2020-04-01 00:00:00
        String endOfMonth = DateUtil.endOfMonth(now).toString(format);//2020-04-30 23:59:59
        ReportData data = new ReportData();
        data.setReportDate(nowStr);
//        -- 今日新增会员数：注册日期是今天
        data.setTodayNewMember(memberDao.findMemberCountByDate(nowStr));
//        -- 总会员数：整个会员表所有的数据
        data.setTotalMember(memberDao.findMemberTotalCount());
//        -- 本周新增会员数：注册日期大于本周一（难点：怎么获取本周一）
        data.setThisWeekNewMember(memberDao.findMemberCountAfterDate(beginOfWeek));
//        -- 本月新增会员数：注册日期大于本月一号（难点：怎么获取本月一号）
        data.setThisMonthNewMember(memberDao.findMemberCountAfterDate(beginOfMonth));
//        -- 今日预约数：预约时间是今天
        data.setTodayOrderNumber(orderDao.findOrderCountByDate(nowStr));
//        -- 今日到诊数：预约时间是今天并且状态是已到诊
        data.setTodayVisitsNumber(orderDao.findVisitsCountByDate(nowStr));
//        -- 本周预约数：预约日期在本周一到本周日之间的（难点：怎么获取本周一，本周日）
        data.setThisWeekOrderNumber(orderDao.findOrderCountBetweenDate(beginOfWeek,endOfWeek));
//        -- 本周到诊数：预约日期在本周一到本周日之间的并且状态是已到诊
        data.setThisWeekVisitsNumber(orderDao.findVisitsCountBetweenDate(beginOfWeek,endOfWeek));
//        -- 本月预约数：预约日期在本月1号到本月的最后一天之间的（难点：怎么获取本月1号，本月最后一天）
        data.setThisMonthOrderNumber(orderDao.findOrderCountBetweenDate(beginOfMonth,endOfMonth));
//        -- 本月到诊数：预约日期在本月1号到本月的最后一天之间的并且状态是已到诊
        data.setThisMonthVisitsNumber(orderDao.findVisitsCountBetweenDate(beginOfMonth,endOfMonth));
//        -- 热门套餐：预约数量靠前的（可以把每个套餐的预约数量统计出来，然后从大到小排序，再取前面几个）
        data.setHotSetmeal(orderDao.findHotSetmeal());
        return data;
    }


    @Override
    public List getMemberByAgeAndGender() {

        ArrayList<Object> list = new ArrayList<>();
        ArrayList<Object> list1 = new ArrayList<>();
        // 0-18 、18-30、30-45、45以上
        //0-18有多少人
        Integer start = 0;
        Integer end = 18;
        Integer count0_18= memberDao.getMemberByAge(start,end);
        HashMap<Object, Object> map1 = new HashMap<>();
        map1.put("name","0-18岁");
        map1.put("value",count0_18);
        list1.add(map1);

        //18-30有多少人
        start = end;
        end = 30;
        Integer count18_30= memberDao.getMemberByAge(start,end);
        HashMap<Object, Object> map2 = new HashMap<>();
        map2.put("name","18-30岁");
        map2.put("value",count18_30);
        list1.add(map2);

        //30-45有多少人
        start = end;
        end = 45;
        Integer count30_45= memberDao.getMemberByAge(start,end);
        HashMap<Object, Object> map3 = new HashMap<>();
        map3.put("name","30-45岁");
        map3.put("value",count30_45);
        list1.add(map3);

        //45-200有多少人
        start = end;
        end = 200;
        Integer count45_200= memberDao.getMemberByAge(start,end);
        HashMap<Object, Object> map4 = new HashMap<>();
        map4.put("name","45岁以上");
        map4.put("value",count45_200);
        list1.add(map4);

        //未知有多少人

        Integer countUnKnow= memberDao.getMemberByAgeUnKnow();
        HashMap<Object, Object> map5 = new HashMap<>();
        map5.put("name","年龄未知");
        map5.put("value",countUnKnow);
        list1.add(map5);
        //将map添加到一个list中
        List<Map> list2 = memberDao.getMemberByGender();

        //将两个list返回
        list.add(list1);
        list.add(list2);
        return list;
    }

    public static void main(String[] args) {
        //获取本周一  本周日 获取本月一号  本月最后一天
        String format = "yyyy-MM-dd";
        DateTime now = new DateTime();
        String nowStr = now.toString(format);

        String beginOfWeek = DateUtil.beginOfWeek(now).toString(format);//2020-04-13 00:00:00
        String endOfWeek = DateUtil.endOfWeek(now).toString(format);//2020-04-19 23:59:59

        String beginOfMonth = DateUtil.beginOfMonth(now).toString(format);//2020-04-01 00:00:00
        String endOfMonth = DateUtil.endOfMonth(now).toString(format);//2020-04-30 23:59:59
    }
}
